using UnityEngine;

namespace NodeCanvas.DialogueTrees
{
	[AddComponentMenu("")]
	[Name("Condition")]
	[Description("Execute the first child node if a Condition is true, or the second one if that Condition is false. The Actor selected is used for the Condition check")]
	public class DLGConditionNode : DLGNodeBase, ITaskAssignable<ConditionTask>, ITaskAssignable
	{
		[SerializeField]
		private Object _condition;

		[HideInInspector]
		public Task task
		{
			get
			{
				return condition;
			}
			set
			{
				condition = (ConditionTask)value;
			}
		}

		public Object serializedTask
		{
			get
			{
				return _condition;
			}
		}

		private ConditionTask condition
		{
			get
			{
				return _condition as ConditionTask;
			}
			set
			{
				_condition = value;
				if (value != null)
				{
					value.SetOwnerSystem(this);
				}
			}
		}

		public override string nodeName
		{
			get
			{
				return base.nodeName + " " + base.finalActorName;
			}
		}

		public override int maxOutConnections
		{
			get
			{
				return 2;
			}
		}

		protected override Status OnExecute()
		{
			if (base.outConnections.Count == 0)
			{
				base.DLGTree.StopGraph();
				return Error("There are no connections.");
			}
			if (!base.finalActor)
			{
				base.DLGTree.StopGraph();
				return Error("Actor not found");
			}
			if (!condition)
			{
				Debug.LogWarning("No Condition on Dialoge Condition Node ID " + base.ID);
				base.outConnections[0].Execute(base.finalActor, base.finalBlackboard);
				return Status.Success;
			}
			if (condition.CheckCondition(base.finalActor, base.finalBlackboard))
			{
				base.outConnections[0].Execute(base.finalActor, base.finalBlackboard);
				return Status.Success;
			}
			if (base.outConnections.Count == 2)
			{
				base.outConnections[1].Execute(base.finalActor, base.finalBlackboard);
				return Status.Failure;
			}
			base.graph.StopGraph();
			return Status.Success;
		}
	}
}
